package com.ruoyi.energy.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.domain.vo.NyMeterAccountExcelVO;
import com.ruoyi.energy.service.INyMeterAccountService;
import com.ruoyi.omp.api.domain.NyMeterAccount;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 仪表台账Controller
 *
 * @author cjie
 * @date 2021-07-29
 */
@Api(tags = " 仪表台账--已废弃")
@RestController
@RequestMapping("/nyMeterAccount")
public class NyMeterAccountController extends BaseController {
    @Autowired
    private INyMeterAccountService nyMeterAccountService;
    @Autowired
    private RemoteBmCodeService remoteBmCodeService;
    /**
     * 查询仪表台账列表
     */
    @ApiOperation(value="查询仪表台账列表", notes="查询仪表台账列表")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount:list")
    @GetMapping("/list")
    public TableDataInfo list(NyMeterAccount nyMeterAccount) {
        startPage();
        List<NyMeterAccount> list = nyMeterAccountService.selectNyMeterAccountList(nyMeterAccount);
        for(NyMeterAccount account:list){
            account.setMeterNumNew(account.getMeterName()+"-"+account.getParkNum()+"-"+ account.getBuildingNum());
        }
        return getDataTable(list);
    }

    /**
     * 查询仪表台账列表
     * @param nyMeterAccount 仪表台账
     * @return
     */
    @PostMapping("/listMeter")
    R<List<NyMeterAccount>> listMeter(NyMeterAccount nyMeterAccount){
        List<NyMeterAccount> list = nyMeterAccountService.selectNyMeterAccountList(nyMeterAccount);
        return R.ok(list);
    }
    /**
     * 导出仪表台账列表
     */
    @ApiOperation(value="导出仪表台账列表", notes="导出仪表台账列表")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount:export")
    @Log(title = "仪表台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NyMeterAccount nyMeterAccount) throws IOException {
        List<NyMeterAccount> list = nyMeterAccountService.selectNyMeterAccountList(nyMeterAccount);
        ExcelUtil<NyMeterAccount> util = new ExcelUtil<NyMeterAccount>(NyMeterAccount.class);
        util.exportExcel(response, list, "仪表台账数据");
    }
    /**
     * 获取仪表台账详细信息
     */
    @ApiOperation(value="获取仪表台账详细信息", notes="获取仪表台账详细信息")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(nyMeterAccountService.selectNyMeterAccountById(id));
    }

    /**
     * 新增仪表台账
     */
    @ApiOperation(value="新增仪表台账", notes="新增仪表台账")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount:add")
    @Log(title = "仪表台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NyMeterAccount nyMeterAccount) {
        RemoteSysBmType remoteSysBmType= remoteBmCodeService.genCode(nyMeterAccount.getSysBmId(), SysBmCodeTypeValue.DEVICE);
        String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
        String deviceType = remoteSysBmType.getHierarchy();//层级
        nyMeterAccount.setMeterNum(nowCode);
        nyMeterAccount.setDeviceType(deviceType);
        return toAjax(nyMeterAccountService.insertNyMeterAccount(nyMeterAccount));
    }

    /**
     * 修改仪表台账
     */
    @ApiOperation(value="修改仪表台账", notes="修改仪表台账")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount:edit")
    @Log(title = "仪表台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NyMeterAccount nyMeterAccount) {
        return toAjax(nyMeterAccountService.updateNyMeterAccount(nyMeterAccount));
    }

    //修改仪表台账，后台调用
    @PostMapping(value = "/update")
    R updateNyMeterAccount(@RequestBody NyMeterAccount nyMeterAccount){
        return toR(nyMeterAccountService.updateNyMeterAccount(nyMeterAccount));
    }
    /**
     * 删除仪表台账
     */
    @ApiOperation(value="删除仪表台账", notes="删除仪表台账")
    @ApiImplicitParam(name = "ids", value = "仪表台账ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "energy:nyMeterAccount:remove")
    @Log(title = "仪表台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(nyMeterAccountService.deleteNyMeterAccountByIds(ids));
    }

    //根据id查找节点及子节点树
    @ApiOperation(value="查找节点树", notes="查找节点树")
    @GetMapping(value = "/tree/{id}")
    public AjaxResult getTreeInfo(@PathVariable("id") String id) {
        return AjaxResult.success(nyMeterAccountService.selectRoot(id));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<NyMeterAccountExcelVO> util = new ExcelUtil<NyMeterAccountExcelVO>(NyMeterAccountExcelVO.class);
        util.importTemplateExcel(response, "仪表数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @PreAuthorize(hasPermi = "energy:nyMeterAccount:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String meterType) throws Exception {
        ExcelUtil<NyMeterAccountExcelVO> util = new ExcelUtil<NyMeterAccountExcelVO>(NyMeterAccountExcelVO.class);
        List<NyMeterAccountExcelVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = nyMeterAccountService.importData(buiRoomVOList,meterType);
        return AjaxResult.success(message);

    }
}
